import {EasyLoading} from "react-native-easy-loading";
import NetUtil from "./NetUtil";
import codePush from 'react-native-code-push'

export default class CodePushUtil {
    static async checkUpdate() {
        let deploymentKey = await CodePushUtil.getDelpoymentKey();
        codePush.checkForUpdate().then(update => {
            if (update) {
                codePush.sync({
                        deploymentKey: deploymentKey,
                        updateDialog: false,
                        installMode: codePush.InstallMode.IMMEDIATE
                    },
                    (status) => {
                        switch (status) {
                            case codePush.SyncStatus.DOWNLOADING_PACKAGE:
                                break;
                            case codePush.SyncStatus.INSTALLING_UPDATE:
                                break;
                        }
                    },
                    (progress) => {
                        let downloadPercent = progress.receivedBytes / progress.totalBytes * 100;
                    });
            }
        })
    }

    static getDelpoymentKey() {
        return new Promise((res,rej)=>{
            NetUtil.requestData('codePush/key',{
                platform:'merchant'
            }).then(result=>{
                res(result.key);
            });
        })
    }
}